#ifndef DEVICESERIALPORTCONTROL_H
#define DEVICESERIALPORTCONTROL_H
#include <QModbusRtuSerialMaster>
#include <QModbusReply>
#include <QSerialPort>
#include <QMainWindow>
#include "maininterface.h"
#include<QTimer>
#include<QDebug>
namespace Ui {
class DeviceSerialportControl;
}

class DeviceSerialportControl : public QMainWindow
{
    Q_OBJECT

public:
    explicit DeviceSerialportControl(QWidget *parent = nullptr);
    ~DeviceSerialportControl();
    static DeviceSerialportControl* instance();
    bool setDeviceStatusLEDONorOff(int address,bool status,bool MainSwitch);
public slots:
    void QTimerReadModsimData(bool modsimDeviceSwitch);
private:
    void initModbusDevice();
    bool isconnectDevice();//是否连接串口
    void disConnectDevice();//断连设备
    bool setDeviceStatus(int device);
    bool triggerReconnection(bool ok);
    bool writeCoil(QModbusRtuSerialMaster *modbusDevice,int address=0,int number=1,bool status=true);
    bool readCoil(QModbusRtuSerialMaster*modbusDevice);
    bool getCurrentAutoControl();
    void set_Timer();
private:
    Ui::DeviceSerialportControl *ui;
    QModbusRtuSerialMaster *modbusDevice;
    QString portName;
    bool connectStatus=false;
    static int reconnectAttempts;//重连次数
    const int maximumReconnectAttempts=3;
    QTimer *m_timer;

private slots:
    bool reConnectDevice();
    void onReadFinishCoil();

signals:
void reConnectDeviceSignal();
void mainSwitch_LEDChanged(int index,bool status);
void IrrigationEquipment_LEDChanged(int index,bool status);
void ventilatingEquipment_LEDChanged(int index,bool status);
void CO2Interpolator_LEDChanged(int index,bool status);
void rollUpDoor_LEDChanged(int index,bool status);
void lightingEquipment_LEDChanged(int index,bool status);
void heatingEquipment_LEDChanged(int index,bool status);
void airCooler_LEDChanged(int index,bool status);
void warningLedChanged(int index,bool status);




};

#endif // DEVICESERIALPORTCONTROL_H
